//
//  FQDColumnArticleCell.m
//  FakeQDaily
//
//  Created by mewxiv on 8/29/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDColumnArticleCell.h"
#import "UIButton+FQDExtension.h"
#import "NSDate+FQDExtension.h"
#import "FQDRestModel.h"
#import "FQDRegularFeedCellItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDColumnArticleCell ()

// 文章图片
@property (nonatomic, strong) UIImageView *articleImageView;
// 标题label
@property (nonatomic, strong) UILabel *titleLabel;
// 描述label
@property (nonatomic, strong) UILabel *descLabel;
// 评论按钮
@property (nonatomic, strong) UIButton *commentButton;
// 点赞按钮
@property (nonatomic, strong) UIButton *praiseButton;
// 日期Label
@property (nonatomic, strong) UILabel *dateLabel;

@end

@implementation FQDColumnArticleCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    
    _articleImageView = [[UIImageView alloc] init];
    _articleImageView.contentMode = UIViewContentModeScaleAspectFill;
    _articleImageView.clipsToBounds = YES;
    [self.contentView addSubview:_articleImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(14.0f);
    _titleLabel.textColor = FQDFeedTitleTextColor;
    _titleLabel.textAlignment = NSTextAlignmentNatural;
    _titleLabel.numberOfLines = 2;
    _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _titleLabel.preferredMaxLayoutWidth = 258.0f;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.clipsToBounds = YES;
    [self.contentView addSubview:_titleLabel];
    
    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FQDRegularFontWithSize(13.0f);
    _descLabel.textColor = FQDFeedDetailTextColor;
    _descLabel.textAlignment = NSTextAlignmentNatural;
    _descLabel.numberOfLines = 2;
    _descLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _descLabel.preferredMaxLayoutWidth = 300.0f;
    _descLabel.backgroundColor = [UIColor whiteColor];
    _descLabel.clipsToBounds = YES;
    [self.contentView addSubview:_descLabel];
    
    _commentButton = [UIButton commentButtonWithCommentNum:17];
    _commentButton.titleLabel.backgroundColor = [UIColor whiteColor];
//    _commentButton.titleLabel.clipsToBounds = YES;
    [self.contentView addSubview:_commentButton];
    
    _praiseButton = [UIButton praiseButtonWithPraiseNum:217];
    _praiseButton.titleLabel.backgroundColor = [UIColor whiteColor];
//    _praiseButton.clipsToBounds = YES;
    [self.contentView addSubview:_praiseButton];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.text = @"123";
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textColor = FQDFeedDetailTextColor;
    _dateLabel.backgroundColor = [UIColor whiteColor];
    _dateLabel.clipsToBounds = YES;
    [self.contentView addSubview:_dateLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_articleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView).offset(1);
        make.right.equalTo(weakSelf.contentView).offset(-1);
        make.height.equalTo(@149);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.articleImageView.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.contentView).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-10);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(8);
        make.left.equalTo(weakSelf.titleLabel.mas_left);
        make.right.equalTo(weakSelf.titleLabel.mas_right);
    }];
    
    [_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(13);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(-10);
    }];
    
    [_praiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.commentButton.mas_right).offset(10);
        make.bottom.equalTo(weakSelf.commentButton);
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.praiseButton.mas_right).offset(8);
        make.centerY.equalTo(weakSelf.praiseButton);
    }];
}

- (void)configureCellWithCellItem:(FQDRegularFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDFeed *feed = cellItem.feed;
    FQDPost *post = feed.post;
    [self.articleImageView sd_setImageWithURL:[NSURL URLWithString:feed.image]];
    [self.titleLabel setText:post.title];
    [self.descLabel setText:post.desc];
    
    [self.commentButton setTitle:[NSString stringWithFormat:@"%@", @(post.comment_count)]
                                          forState:UIControlStateNormal];
    [self.praiseButton setTitle:[NSString stringWithFormat:@"%@", @(post.praise_count)]
                                         forState:UIControlStateNormal];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:post.publish_time];
    [self.dateLabel setText:[date dateStringSinceNow]];
}

#pragma mark Getters and Setters
+ (NSString *)reuseId
{
    return @"FQDColumnArticleCellId";
}

@end
